package cn.detu.ln.controller;

import cn.detu.ln.entity.Teacher;
import cn.detu.ln.service.TeacherService;
import cn.detu.ln.utils.PageUtil;
import cn.detu.ln.utils.RestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("teacher")
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    // GET请求获取用户列表数据
    // localhost:1314/user/selectAll
    @GetMapping("{url}")
    public RestUtil userGet(@PathVariable String url, Teacher teacher, PageUtil pageUtil, String search) {
        switch (url) {
            case "selectAll":
                return teacherService.selectAll();
            case "selectOne": return teacherService.select(teacher.getTeaId());
            case "teacherDelete":
                return teacherService.teacherDelete(teacher.getTeaId());
            case "queryTeacherByPage":
                return teacherService.queryTeacherByPage(pageUtil.getCurrent(), pageUtil.getRows(),
                        search);
            // 按工号查询
            case "queryTeacherByTeaIdPage":return teacherService.queryTeacherByTeaIdPage(pageUtil.getCurrent(),
                    pageUtil.getRows(),
                    search);
            // 按职称查询
            case "queryTeacherByTeaTitlePage":return teacherService.queryTeacherByTeaTitlePage(pageUtil.getCurrent(),
                    pageUtil.getRows(),
                    search);
        }

        return null;
    }

    @PostMapping("{url}")
    public RestUtil userPost(@PathVariable String url, Teacher teacher,String newPwd) {
        switch (url) {
            case "teacherUpdate":
                return teacherService.teacherUpdate(teacher);
            case "teacherInsert":
                return teacherService.teacherInsert(teacher);
            // 实现登录
            case "login":
                return teacherService.login(teacher);
            case "updatePwd":return teacherService.teacherUpdatePwd(teacher,newPwd);
        }
        return null;
    }

    // GET请求根据userId获取单条数据
    // localhost:1315/course/queryCourseById/1001
    @GetMapping("queryTeacherById/{teaId}")
    public Teacher queryTeacherById(@PathVariable("teaId") Integer teaId) {
        return teacherService.queryTeacherById(teaId);
    }

}
